package org.esgi.transformer.impl;

import java.util.ArrayList;
import java.util.List;

import org.esgi.model.impl.Form;
import org.esgi.model.interfaces.IForm;
import org.esgi.model.interfaces.IQuestion;

import org.esgi.transformer.interfaces.IXMLFormTransformer;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLFormTransformer extends XMLModelTransformer implements IXMLFormTransformer
{
	private XMLQuestionTransformer questionTransformer_;
	
	public static final String XMLFormNodeName = "form";
	public static final String XMLFormIdAttrName = "id";
	public static final String XMLFormNameNodeName = "name";
	public static final String XMLFormQuestionsNodeName = "questions";

	public XMLFormTransformer(XMLQuestionTransformer questionTransformer)
	{
		questionTransformer_ = questionTransformer;
	}

	@Override
	public IForm formFromElement(Element e)
	{
		IForm f = new Form();
		f.setId(e.getAttribute(this.addDefaultNamespaceToName(XMLFormIdAttrName)));
		NodeList nl = e.getChildNodes();
		for(int i=0 ; i<nl.getLength(); i++)
		{
			Node childNode = nl.item(i);
			if(childNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLFormNameNodeName)))
			{
				Node textNode = childNode.getFirstChild();
				if(textNode != null)
					f.setName(textNode.getNodeValue());
			}
			else if(childNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLFormQuestionsNodeName)))
			{
				NodeList nlQuestions = childNode.getChildNodes();
				if(nlQuestions != null)
				{
					ArrayList<IQuestion> questions = new ArrayList<IQuestion>();
					for(int j=0 ; j<nlQuestions.getLength(); j++)
					{
						Node questionNode = nlQuestions.item(j);
						if(questionNode.getNodeName().equals(this.addDefaultNamespaceToName(XMLQuestionTransformer.XMLQuestionNodeName)))
							questions.add(this.questionTransformer_.questionFromElement((Element) questionNode));
					}
					f.setQuestions(questions);
				}
			}
		}
		return f;
	}

	@Override
	public Document formToDocument(IForm aForm, DOMImplementation impl)
	{
		if((aForm instanceof IForm) && impl != null)
		{
			Document doc = impl.createDocument("", this.addDefaultNamespaceToName(XMLFormNodeName), null);
			if(doc != null)
			{
				Element formNode = doc.getDocumentElement();
				formNode.setAttribute(this.addDefaultNamespaceToName(XMLFormIdAttrName), aForm.getId());
				
				Element nameNode = doc.createElement(this.addDefaultNamespaceToName(XMLFormNameNodeName));
				nameNode.appendChild(doc.createTextNode(aForm.getName()));
				formNode.appendChild(nameNode);
				
				Node questionsNode = doc.createElement(this.addDefaultNamespaceToName(XMLFormQuestionsNodeName));
				List<IQuestion> questions = aForm.getQuestions();
				if(questions != null)
				{
					for(int i=0; i<questions.size(); i++)
					{
						Element questionNode = this.questionTransformer_.questionToElement(questions.get(i), doc);
						if(questionNode != null)
							questionsNode.appendChild(questionNode);
					}
				}
				formNode.appendChild(questionsNode);
				return doc;
			}
		}
		return null;
	}
}
